package com.crud.demo1.demos.controller;

import com.crud.demo1.demos.Service.ITProjectService;
import com.crud.demo1.demos.commons.AjaxResult;
import com.crud.demo1.demos.entity.TProject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhangNing
 * @since 2025-02-26
 */
@Api(tags = "建筑工程项目")
@RestController
@RequestMapping("/tProject")
public class TProjectController {

    private final ITProjectService tProjectService;

    @Autowired
    public TProjectController(ITProjectService tProjectService) {
        this.tProjectService = tProjectService;
    }

    @ApiOperation(value = "getProList",notes = "查询所有工程项目")
    @GetMapping("getProList")
    public AjaxResult getProList(){
        List<TProject> projectList = tProjectService.list();
        return AjaxResult.ok(projectList);
    }

    @ApiOperation(value = "addProject",notes = "添加新的工程项目")
    @PostMapping("addProject")
    public AjaxResult addProject(@RequestBody @Valid TProject tProject){
        boolean result = tProjectService.save(tProject);
        if (result){
            return AjaxResult.ok();
        }
        return AjaxResult.fail();
    }

    @ApiOperation(value = "updateProject",notes = "修改工程项目信息")
    @PostMapping("updateProject")
    public AjaxResult updateProject(@RequestBody @Valid TProject tProject){
        boolean result = tProjectService.saveOrUpdate(tProject);
        if (result){
            return AjaxResult.ok();
        }
        return AjaxResult.fail();
    }

    @ApiOperation(value = "delProject",notes = "删除工程项目信息")
    @DeleteMapping("delProject")
    public AjaxResult delProject(@RequestBody @Valid TProject tProject){
        boolean result = tProjectService.removeById(tProject);
        if(result){
            return AjaxResult.ok();
        }
        return AjaxResult.fail();
    }
}
